package com.logistics.controller.tenant;

import com.logistics.common.Result;
import com.logistics.entity.SysDictionary;
import com.logistics.service.DictionaryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租户端 - 字典管理控制器
 */
@RestController
@RequestMapping("/tenant/dictionary")
public class DictionaryController {
    
    private final DictionaryService dictionaryService;
    
    public DictionaryController(DictionaryService dictionaryService) {
        this.dictionaryService = dictionaryService;
    }
    
    /**
     * 获取某类型的字典列表（仅返回启用的）
     */
    @GetMapping("/{type}")
    @PreAuthorize("hasAuthority('d8l4h7p2') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<List<SysDictionary>> getDictionaryByType(@PathVariable String type) {
        try {
            List<SysDictionary> list = dictionaryService.getDictionaryByType(type);
            return Result.success(list);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取某类型的所有字典列表（包括已禁用的）
     * 用于显示历史数据
     */
    @GetMapping("/{type}/all")
    @PreAuthorize("hasAuthority('d8l4h7p2') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<List<SysDictionary>> getAllDictionaryByType(@PathVariable String type) {
        try {
            List<SysDictionary> list = dictionaryService.getAllDictionaryByType(type);
            return Result.success(list);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取所有字典
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('d8l4h7p2') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<List<SysDictionary>> getAllDictionaries() {
        try {
            List<SysDictionary> list = dictionaryService.getAllDictionaries();
            return Result.success(list);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 添加自定义字典
     */
    @PostMapping
    @PreAuthorize("hasAuthority('f2j6v9k3') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<SysDictionary> addDictionary(@RequestBody SysDictionary dictionary) {
        try {
            SysDictionary result = dictionaryService.addDictionary(dictionary);
            return Result.success("添加成功", result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新字典
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('g5x8n1w7') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<String> updateDictionary(@PathVariable Long id, 
                                           @RequestBody SysDictionary dictionary) {
        try {
            dictionaryService.updateDictionary(id, dictionary);
            return Result.success("更新成功", null);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除字典
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('h9c4z6m2') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<String> deleteDictionary(@PathVariable Long id) {
        try {
            dictionaryService.deleteDictionary(id);
            return Result.success("删除成功", null);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}

